//
//  PhotoBrowserViewCell.m
//  etionUI
//
//  Created by WangJian on 14-2-14.
//  Copyright (c) 2014年 GuangZhouXuanWu. All rights reserved.
//

#import "PhotoBrowserViewCell.h"
#import "HPGrowingTextView.h"
#import "CircleButton.h"
#import "UserGuideManager.h"
#import "UserGuideView.h"

#define BTN_SIZE 32
#define BTN_BORDER_DISTANCE 15

@implementation CPhotoBrowserViewCellInfo

- (void)dealloc
{
    [_remark release];
    [super dealloc];
}

@end

@interface CPhotoBrowserViewCell() <HPGrowingTextViewDelegate>

@property (nonatomic, retain) HPGrowingTextView *remarkView;
@property (nonatomic, retain) UIView *remarkBgView;
@property (nonatomic, retain) UIView *tapBgView;

//@property (nonatomic, retain) CCircleButton *cancelBtn;
//@property (nonatomic, retain) CCircleButton *saveBtn;

@property (nonatomic, copy) NSString *oldRemark;

@end

@implementation CPhotoBrowserViewCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.remarkView = [[[HPGrowingTextView alloc] initWithFrame:CGRectMake(0, self.height, self.width, 20)] autorelease];
        self.remarkView.delegate = self;
        self.remarkView.minNumberOfLines = 1;
        self.remarkView.maxNumberOfLines = 4;
        self.remarkView.returnKeyType = UIReturnKeyDone;
        self.remarkView.font = [UIFont systemFontOfSize:14.0f];
        self.remarkView.backgroundColor = [UIColor clearColor];
        self.remarkView.textColor = [UIColor whiteColor];
        self.remarkView.contentInset = UIEdgeInsetsMake(0, 15, 0, 15);
        self.remarkView.editable = NO;
        
        self.remarkBgView = [[[UIView alloc] initWithFrame:self.remarkView.frame] autorelease];
        self.remarkBgView.backgroundColor = [UIColor blackColor];
        self.remarkBgView.alpha = .5;
        
        self.tapBgView = [[[UIView alloc] initWithFrame:self.bounds] autorelease];
        UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgViewTrigger:)] autorelease];
        [self.tapBgView addGestureRecognizer:tapGesture];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_remark release];
    [_oldRemark release];
    [_remarkView release];
    [_remarkBgView release];
    [_tapBgView release];
    [super dealloc];
}

- (void)updateWithCellInfo:(id)cellInfo
{
    CPhotoBrowserViewCellInfo *info = cellInfo;
    [super updateWithCellInfo:info];
    self.remark = info.remark;
    self.remarkOffset = info.remarkOffset;
}

- (void)setRemarkShowState:(BOOL)isShow
{
    [UIView animateWithDuration:.25 animations:^
    {
        self.remarkOffset = isShow ? 55 : -self.remarkView.height;
    }];
}

- (void)addRemarkView
{
    [self addRemarkViewWithDuration:0.0 options:0 completion:^(BOOL finished) {}];
}

- (void)addRemarkViewWithDuration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion
{
    if (self.remarkView.superview == nil)
    {
        [self addSubview:self.remarkBgView];
        [self addSubview:self.remarkView];
        self.remarkView.top = self.height - self.remarkView.height-self.remarkOffset;
        self.remarkBgView.frame = CGRectMake(0, self.remarkView.top, self.width, self.remarkView.height+self.remarkOffset);
        [UIView animateWithDuration:duration delay:.0 options:options animations:^
        {
            self.remarkBgView.alpha = 0.5;
        } completion:^(BOOL finished)
        {
            completion(finished);
        }];
        
    }
}

- (void)removeRemarkView
{
    [self removeRemarkViewWithDuration:0.0 options:0 completion:^(BOOL finished) {}];
}

- (void)removeRemarkViewWithDuration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options completion:(void (^)(BOOL finished))completion
{
    [UIView animateWithDuration:duration delay:.0 options:options animations:^
    {
        self.remarkBgView.alpha = 0.0;
    } completion:^(BOOL finished)
    {
        [self.remarkView removeFromSuperview];
        [self.remarkBgView removeFromSuperview];
        completion(finished);
    }];
}

- (void)setRemarkOffset:(CGFloat)remarkOffset
{
    _remarkOffset = remarkOffset;
    self.remarkView.frame = CGRectMake(self.remarkView.left, self.bottom-remarkOffset-self.remarkView.height, self.remarkView.width, self.remarkView.height);
    self.remarkBgView.frame = CGRectMake(self.remarkView.left, self.remarkView.top, self.remarkView.width, self.remarkView.height+remarkOffset);
}

- (void)setRemark:(NSString *)remark
{
    NSString *tmp = [remark copy];
    [_remark release];
    _remark = tmp;
    
    [self.remarkView setText:remark];
    if (remark.length > 0)
    {
        [self.remarkView scrollRangeToVisible:NSMakeRange(0, 1)];
        if (self.remarkView.superview == nil)
        {
            [self addRemarkView];
        }
    }
    else
    {
        if (self.remarkView.superview != nil)
        {
        [self removeRemarkView];
    }
}
}

- (void)editRemark
{
    [self startEditRemark];
}

- (void)startEditRemark
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    self.oldRemark = self.remark;
    
    self.remarkView.editable = YES;
    if (self.remarkView.superview == nil)
    {
        [self addRemarkView];
    }
    [self.remarkView becomeFirstResponder];
    [self.remarkView scrollRangeToVisible:NSMakeRange(self.remark.length-1, 1)];
    [self insertSubview:self.tapBgView belowSubview:self.remarkBgView];
    [self.delegate photoBrowserViewCellStartEditRemark:self];
}

- (void)endEditRemark
{
    [self.remarkView resignFirstResponder];
    [self.tapBgView removeFromSuperview];
    self.remarkView.editable = NO;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [self.delegate photoBrowserViewCellEndEditRemark:self];
}

- (void)tapBgViewTrigger:(UITapGestureRecognizer *)gesture
{
    [self cancelEdit];
}

- (BOOL)growingTextViewShouldReturn:(HPGrowingTextView *)growingTextView
{
    [self saveEdit];
    return NO;
}

- (void)cancelEdit
{
    self.remark = self.oldRemark;
    [self endEditRemark];
}

- (void)saveEdit
{
    self.remark = self.remarkView.text;
//    _remark = [self.remarkView.text copy];
    [self endEditRemark];
    [self.delegate photoBrowserViewCellRemarkChange:self];
}

#pragma mark - growing text view delegate

- (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(CGFloat)height
{
    self.remarkView.top = self.remarkView.bottom-height;
    self.remarkBgView.frame = CGRectMake(0, self.remarkView.top, self.width, height+self.remarkOffset);
}

- (void)keyboardWillShow:(NSNotification *)noti
{
    CGRect frame = [(NSValue *) [[noti userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSTimeInterval duration = [[[noti userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationOptions options = [[[noti userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
    [UIView animateWithDuration:duration delay:0 options:options animations:^{
        self.remarkView.top = CGRectGetMinY(frame) - self.remarkView.height;
        self.remarkBgView.top = self.remarkView.top;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)keyboardWillHide:(NSNotification *)noti
{
    NSTimeInterval duration = [[[noti userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationOptions options = [[[noti userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
    [UIView animateWithDuration:duration delay:0 options:options animations:^{
        self.remarkView.top = self.height - self.remarkView.height - self.remarkOffset;
        self.remarkBgView.top = self.remarkView.top;
    } completion:^(BOOL finished) {
        
    }];
}

@end
